package com.yaopaikeji.dao;

import com.yaopaikeji.constants.Constants;
import com.yaopaikeji.exception.InsertException;
import com.yaopaikeji.mapper.RoleInfoMapper;
import com.yaopaikeji.model.RoleInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleInfoDao extends BaseDao<RoleInfo>{

    private static Logger logger = LoggerFactory.getLogger(RoleInfoDao.class);

    @Autowired
    private RoleInfoMapper roleInfoMapper;

    @Override
    public RoleInfo findByPrimaryKey(int id) {

        return roleInfoMapper.findByPrimaryKey(id);
    }

    @Override
    public List<RoleInfo> findAll() {

        return roleInfoMapper.findAll();
    }

    public RoleInfo findByRoleName(String roleName){

        return roleInfoMapper.findByRoleName(roleName);
    }

    public RoleInfo saveRoleInfo(RoleInfo roleInfo){

        if(roleInfoMapper.saveRoleInfo(roleInfo) == 1)
            return roleInfo;

        logger.error("插入数据失败");
        throw new InsertException(String.format(Constants.INSERT_ERROR_FMT, "角色信息"));
    }

    public List<String> findAllRoleNames(){

        return roleInfoMapper.findAllRoleNames();
    }


}
